package cn.dgut.training.controller;

import cn.dgut.training.annotation.LoginUser;
import cn.dgut.training.dto.goods.GoodsAllDTO;
import cn.dgut.training.dto.goods.GoodsListDTO;
import cn.dgut.training.entity.goods.Goods;
import cn.dgut.training.entity.goods.GoodsProduct;
import cn.dgut.training.service.IGoodsProductService;
import cn.dgut.training.service.impl.CategoryServiceImpl;
import cn.dgut.training.service.impl.GoodsServiceImpl;
import cn.dgut.training.vo.CommonResponse;
import cn.dgut.training.vo.goods.GoodsAddCategoryVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
@Slf4j
public class GoodsController {

    @Autowired
    private GoodsServiceImpl goodsService;

    @Autowired
    private CategoryServiceImpl categoryService;

    @Autowired
    private IGoodsProductService goodsProductService;

    //后台

    /**
     * 分页显示
     * 1、只显示编号不显示主键
     * 2、查询只查编号和名称
     * @param goodsListDTO
     * @return
     */
    @PostMapping("/list")
    public CommonResponse listGoods(@RequestBody GoodsListDTO goodsListDTO){

        return CommonResponse.success(goodsService.listGoodsByPage(goodsListDTO));
    }


    @PostMapping("/create")
    public CommonResponse add(@RequestBody GoodsAllDTO goodsAllDTO){
        return goodsService.add(goodsAllDTO);
    }

    @PostMapping("/update")
    public CommonResponse edit(@RequestBody GoodsAllDTO goodsAllDTO){

        return goodsService.edit(goodsAllDTO);
    }


    @PostMapping("/delete")
    public CommonResponse delete(@RequestBody Goods goods){

        return goodsService.delete(goods);
    }

    @GetMapping("/detail")
    public CommonResponse detail(Long id){

        return goodsService.detail(id);
    }

    //团购根据goodsName查询商品
    @GetMapping("/findmessage")
    public CommonResponse findGoodsByName(@RequestParam("goodsName") String goodsName){
        return CommonResponse.success(goodsService.getOne(new QueryWrapper<Goods>().lambda().like(Goods::getName,goodsName)));
    }


    //添加商品的时候获取分类信息
    @GetMapping("/catAndBrand")
    public CommonResponse catAndBrand(){

        List<GoodsAddCategoryVO> categoryList=categoryService.getCa();
        Map<String, Object> data = new HashMap<>();
        data.put("categoryList", categoryList);
        return CommonResponse.success(data);
    }


    //小程序端

    //统计商品总数
    @GetMapping("/goodsCount")
    public CommonResponse goodsCount(){
        return CommonResponse.success(goodsService.count());
    }

    //查询是新品的商品
    @GetMapping("/queryByNew")

    public List queryByNew(){
        return goodsService.queryByNew();
    }

    //查询是热门的商品
    @GetMapping("/queryByHot")
    public List queryByHot(){
        return goodsService.queryByNew();
    }

    /**
     * 商品详情
     * 说明：用户可以不登录查看，如果用户登录了就记录用户的足迹以及返回用户收藏信息
     * @param userId
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public CommonResponse getById(@LoginUser Long userId,@RequestParam("id") Long id){

        return goodsService.getById(userId,id);

    }

    /**
     * 根据分类id查询当前分类，父级分类和兄弟分类
     * @param id
     * @return9
     */
    @GetMapping("/category")
    public CommonResponse category(@RequestParam Long id){

        return goodsService.category(id);
    }

    @GetMapping("/standard")
    public CommonResponse standard(@RequestParam Long id){

        return goodsService.standard(id);
    }

    @PostMapping("/getOneGoods")
    public Goods getOneGoods(@RequestParam("goodsId") Long goodsId){
        return goodsService.getById(goodsId);
    }

    @GetMapping("/updateStock")
    public void updateStock(@RequestParam("productId") Long productId,@RequestParam("number") Integer number){
        GoodsProduct goodsProduct=goodsProductService.getById(productId);
        goodsProduct.setStock(goodsProduct.getStock()+number);
        goodsProductService.updateById(goodsProduct);
    }
}
